package ils.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import ils.domain.issue.model.Issue;
import ils.services.IssueService;
import ils.util.SpringUtil;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Set;

public class IssueAction extends ActionSupport implements SessionAware, Preparable
{
    private static final Logger logger = LoggerFactory.getLogger(IssueAction.class);
    private Map<String, Object> session;
    private int                 bookId;
    private int                 userId;
    private String              barcode;
    private String              action;
    private String              msg;
    private String              role;
    private IssueService        issueService;

    public String execute() throws Exception
    {

        try
        {
            Set<String> roles = (Set<String>) session.get("role");
            if(roles.contains("admin")) role = "admin";
            else if(roles.contains("librarian")) role = "librarian";
            else if(roles.contains("user")) role = "user";
            logger.debug("role: " + role);
            if (role == null)
                role = "user";
            if (action == null)
                action = "request";
            if (role.equals("user"))
            {
                Integer id = (Integer) session.get("userId");
                logger.debug("userId: " + id);
                if (id == null)
                {
                    msg = "Please log in before issue book";
                    return LOGIN;
                }
                else
                {
                    userId = id;
                }
            }
            if (action.equals("issue") && role.equals("librarian"))
            {
                Issue issue = issueService.issue(barcode, userId);
                msg = "Book (id: " + issue.getItem().getBookId() + ") has been issued to user: " + userId;
            }
            else if (action.equals("request"))
            {
                issueService.request(bookId, userId);
                msg = "Book (id: " + bookId + ") has been requested by user: " + userId;
            }
            else if (action.equals("return"))
            {
                issueService.returnBack(bookId, userId);
                msg = "Book (id: " + bookId + ") has been returned by user: " + userId;
            }
            else if (action.equals("release") && role.equals("librarian"))
            {
                logger.debug("releasing book: " + barcode);
                Issue issue = issueService.release(barcode, userId);
                msg = "Book (id: " + issue.getItem().getBookId() + ") has been released by user: " + userId;
            }
            logger.debug("issue action: " + msg);
            return SUCCESS;
        }
        catch (Exception e)
        {
            logger.error("some error while issue", e);
            msg = e.getMessage();
            return ERROR;
        }
    }

    @Override
    public void setSession(Map<String, Object> arg0)
    {
        this.session = arg0;
    }

    public int getBookId()
    {
        return bookId;
    }

    public void setBookId(int bookId)
    {
        this.bookId = bookId;
    }

    public int getUserId()
    {
        return userId;
    }

    public void setUserId(int userId)
    {
        this.userId = userId;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }

    public void setIssueService(IssueService issueService)
    {
        this.issueService = issueService;
    }

    public String getBarcode()
    {
        return barcode;
    }

    public void setBarcode(String barcode)
    {
        this.barcode = barcode;
    }

    @Override
    public void prepare() throws Exception {
        issueService = (IssueService) SpringUtil.getApplicationContext().getBean("issueService");
    }
}
